<?php

namespace App\Validation;

use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Numericality;

class DataValidation extends BaseValidation
{
    public function initialize()
    {
		$this->add(
			"id",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"city_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"reco_ids",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"tags",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"title",
			new PresenceOf(
				  [
					  "message" => ":field is required",
				  ]
			)
		);

		$this->add(
			"short_title",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"publish_time",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"summary",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"author",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);
		
		$this->add(
			"cate_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"type_id",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"thumb",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"pics",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"price",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"content",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"doc",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"sources",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"download",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"share",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"visit",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"comment",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"is_free",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_display",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"is_del",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"order_by",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"is_stick",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);

		$this->add(
			"update_time",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);

		$this->add(
			"create_time",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);
		//2017-03-30 +
		$this->add(
			"is_self",
			new Numericality(
				  [
					  "message" => ":field is not numeric",
				  ]
			)
		);
		//2017-03-30 +
		$this->add(
			"three_url",
			new PresenceOf(
				  [
					  "message" => ":field is required",
					  "allowEmpty" => true,
				  ]
			)
		);			
    }
	
	//验证之前
	public function beforeValidation($data, $entity, $messages)
    {
    	$data->is_self = property_exists($data,'is_self') ? $data->is_self : 0;
    	
    	$data->price = property_exists($data,'price') ? $data->price : 0.00;
        $data->sources = property_exists($data,'sources') ? $data->sources : '觅窝';
		// $data->visit = property_exists($data,'visit') ? $data->visit : 0;
		// $data->download = property_exists($data,'download') ? $data->download : 0;
		// $data->share = property_exists($data,'share') ? $data->share : 0;
		// $data->comment = property_exists($data,'comment') ? $data->comment : 0;
		$data->is_free = property_exists($data,'is_free') ? $data->is_free : 1;
		$data->is_display = property_exists($data,'is_display') ? $data->is_display : 1;
		$data->is_del = property_exists($data,'is_del') ? $data->is_del : 0;
		$data->order_by = property_exists($data,'order_by') ? $data->order_by : 100;
		$data->is_stick = property_exists($data,'is_stick') ? $data->is_stick : 0;

		$data->update_time = property_exists($data,'update_time') ? $data->update_time : date('Y-m-d H:i:s', time());
		$data->create_time = property_exists($data,'create_time') ? $data->create_time : date('Y-m-d H:i:s', time());
        return true;
    }
}